<?php
	session_start();
	//引入公共文件
	require('../common/common.php');
	//实例化Model对象
	$model = new Model('bbs_user');
	
	//接收信息 并去掉传值两侧的空白
	$_POST['username'] = trim(isset($_POST['username']) ? $_POST['username'] : '');
	$_POST['password'] = trim(isset($_POST['password']) ? $_POST['password'] : '');
	$_POST['password2'] = trim(isset($_POST['password2']) ? $_POST['password2'] : '');
	$_POST['email'] = trim(isset($_POST['email']) ? $_POST['email'] : '');	
	//判断用户名密码是否为空
	if(empty($_POST['username']) || empty($_POST['password'])) {
		echo "<script>alert('用户名或密码不能为空!');window.history.back(-1);</script>";
		return;
	}
	$name = $_POST['username'];
	$pass = md5($_POST['password']);
	
	$sqlfree1 = "SELECT * FROM `bbs_user` WHERE username = '{$name}' AND password='{$pass}'";

	$res = $model->free($sqlfree1);
	if($res){
		foreach ($res as $info) {}
		/*验证密码*/
		if($info['password'] == $pass){
			$_SESSION['isLogin'] = 1;
			$_SESSION['id'] = $info['id'];
			$_SESSION['username'] = $info['username'];
			$_SESSION['pic'] = $info['pic'];
			$_SESSION['qx'] = $info['qx'];
			echo '<script>alert("登录成功");window.location.href="index.php";</script>';
		}else{
			/*密码不正确*/
			echo '<script>alert("密码错误");window.location.href="index.php";</script>';
		}
	}else{
		/*用户名不存在*/
		echo '<script>alert("用户名不存在");window.location.href="index.php";</script>';
	}